package com.example.administrator.yanfoxconn.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.adapter.KMDMTBoxAdapter;
import com.example.administrator.yanfoxconn.bean.KMDMT;
import com.example.administrator.yanfoxconn.bean.KNZWGreen;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 安全部 常用表單
 * 關務 設備巡檢  共用 如果權限為KK
 * 總務 綠化管理  共用 如果權限為KN
 * Created by song on 2018/11/27.
 */

public class CommonformsActivity extends BaseActivity implements View.OnClickListener{

    private final int MESSAGE_GREEN_AREA = 111;//網絡錯誤
    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.tv_green)
    TextView tvGreen;//總務-綠化描述
    @BindView(R.id.btn_one)
    Button btn_one;//安保部 無紙化辦公 臨時來訪登記
    @BindView(R.id.btn_scan)
    Button btnViolation;//員工違規
    @BindView(R.id.btn_write)
    Button btnTurnover;//員工進出
    @BindView(R.id.btn_ocr)
    Button btnVehicle;//二輪車
    @BindView(R.id.btn_self)
    Button btnSelf;//私家車
    private Context mContext=CommonformsActivity.this;
    private List<KNZWGreen> knArea;
    private String result="";
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cross_mian);
        ButterKnife.bind(this);
        if (FoxContext.getInstance().getType().equals("KK")){
            btnViolation.setText("帳號註冊");
            btnTurnover.setText("設備查詢");
            btnVehicle.setText("設備盤點");
            btnSelf.setText("系統設置");
            tvTitle.setText("關務設備");
            btnSelf.setVisibility(View.VISIBLE);
        }else if (FoxContext.getInstance().getType().equals("KN")){
            btnViolation.setText("作業模式");
            btnTurnover.setText("異動模式");
            btnVehicle.setText("複查模式");
            tvTitle.setText("位置資訊");
            tvGreen.setVisibility(View.VISIBLE);
            result=getIntent().getStringExtra("result");
            getGreenInfo(result);
        }else {
            btn_one.setText("臨時來訪登記");
            btnViolation.setText(getString(R.string.emp_wrong));
            btnTurnover.setText(getString(R.string.emp_turnover));
            btnVehicle.setText(getString(R.string.two_wheel_vehicle));
            btnSelf.setText("私家車違規登記表");
            tvTitle.setText("無紙化辦公");
            btn_one.setVisibility(View.VISIBLE);
            btnSelf.setVisibility(View.VISIBLE);
        }
        btnViolation.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnViolation.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btnTurnover.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnTurnover.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btnVehicle.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnVehicle.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btnSelf.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btnSelf.setTextColor(getResources().getColor(R.color.color_eeeeee));
        btn_one.setBackgroundColor(getResources().getColor(R.color.color_009adb));
        btn_one.setTextColor(getResources().getColor(R.color.color_eeeeee));

        btnBack.setOnClickListener(this);
        btnViolation.setOnClickListener(this);
        btnTurnover.setOnClickListener(this);
        btnVehicle.setOnClickListener(this);
        btnSelf.setOnClickListener(this);
        btn_one.setOnClickListener(this);
    }

    /**
     * 位置信息及綠化面積
     * @param result
     */
    private void getGreenInfo(String result) {
            showDialog();
            String url = Constants.HTTP_GREEN_AREA + "?gId="+result ;
            new Thread() {
                @Override
                public void run() {
                    String result = HttpUtils.queryStringForPost(url);

                    Log.e("---------", "=getcarLog=fff===" + url);
                    Gson gson = new Gson();
                    String response = result;

                    if (result != null) {
                        Log.e("---------", "result==fff===" + response);
                        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                        String errCode = jsonObject.get("errCode").getAsString();
                        if (errCode.equals("200")) {
                            JsonArray array = jsonObject.get("data").getAsJsonArray();
                            knArea = new ArrayList<KNZWGreen>();
                            for (JsonElement type : array) {
                                KNZWGreen humi = gson.fromJson(type, KNZWGreen.class);
                                knArea.add(humi);
                            }
                            Message message = new Message();
                            message.what = MESSAGE_GREEN_AREA;
                            message.obj = jsonObject.get("errMessage").getAsString();
                            mHandler.sendMessage(message);
                        } else {
//                        isShowReturn = false;
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("errMessage").getAsString();
                            mHandler.sendMessage(message);

                        }
                    } else {
                        Message message = new Message();
                        message.what = Constants.MESSAGE_TOAST;
                        message.obj = "網絡問題請求失敗,請重試!";
                        mHandler.sendMessage(message);
                    }
                    dismissDialog();
                }
            }.start();
        }
        private String ids;//获取ID用于上传
        Handler mHandler = new Handler() {
            @SuppressLint("HandlerLeak")
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MESSAGE_GREEN_AREA:
                        String tet="";
                        tet=knArea.get(0).getgArea()+"\n"+knArea.get(0).getgDimId();
                        for (int i=0;i<knArea.size();i++){
                            tet = tet+"\n"+knArea.get(i).getGcNum()+knArea.get(i).getGcUnit();
                        }
                        tvGreen.setText(tet);
                        break;
                    case Constants.MESSAGE_TOAST://Toast彈出
                        ToastUtils.showLong(mContext, msg.obj.toString());
                        break;
                    case Constants.MESSAGE_UP://t提交提示
                        ToastUtils.showLong(mContext, msg.obj.toString());
                        finish();
                        break;
                    case Constants.MESSAGE_NOT_NET://t提交提示
                        ToastUtils.showLong(mContext, R.string.net_mistake);
                        break;
                }
                super.handleMessage(msg);
            }
        };

    @Override
    public void onClick(View view) {
        if (FoxContext.getInstance().getType().equals("KK")){
            switch (view.getId()){
                case R.id.btn_scan://帳號註冊

                    break;
                case R.id.btn_write://設備查詢

                    break;
                case R.id.btn_ocr://設備盤點
                   Intent intent = new Intent(mContext,QrCodeActivity.class);
                    intent.putExtra("title", "掃描二維碼");
                    intent.putExtra("num", "KK");
                    startActivity(intent);
                    break;
                case R.id.btn_self://系統設置

                    break;
                case R.id.btn_title_left:
                    finish();
                    break;
            }
        }else if (FoxContext.getInstance().getType().equals("KN")){
        switch (view.getId()){
            case R.id.btn_scan://作業模式
                Intent intent = new Intent(CommonformsActivity.this,KNGreenProjectActivity.class);
                intent.putExtra("result",result);
                intent.putExtra("gName",knArea.get(0).getgDimId());
                startActivity(intent);
                break;
            case R.id.btn_write://異動模式
                Intent intent2 = new Intent(CommonformsActivity.this,KNAddExceActivity.class);
                intent2.putExtra("result",result);
                intent2.putExtra("gName",knArea.get(0).getgDimId());
                startActivity(intent2);
                break;
            case R.id.btn_ocr://複查模式
                Intent intent3 = new Intent(CommonformsActivity.this,KNCheckExceListActivity.class);
                intent3.putExtra("result",result);
                intent3.putExtra("gName",knArea.get(0).getgDimId());
                startActivity(intent3);
                break;
            case R.id.btn_title_left:
                finish();
                break;
        }
        }else{
                switch (view.getId()){
                    case R.id.btn_one://臨時來訪登記
                        Intent intent1 = new Intent(CommonformsActivity.this,CrossScanMainActivity.class);
                        intent1.putExtra("flag","ko");
                        startActivity(intent1);
                        break;
                    case R.id.btn_scan://員工違規
                        Intent intent = new Intent(CommonformsActivity.this,CrossScanMainActivity.class);
                        intent.putExtra("flag","wrong");
                        startActivity(intent);
                        break;
                    case R.id.btn_write://員工進出
//                Intent intent = new Intent(CommonformsActivity.this,EmpTurnoverActivity.class);
//                startActivity(intent);
                        Intent intent2 = new Intent(CommonformsActivity.this,CrossScanMainActivity.class);
                        intent2.putExtra("flag","inout");
                        startActivity(intent2);

                        break;
                    case R.id.btn_ocr://二輪車違規
                        Intent intent3 = new Intent(CommonformsActivity.this,CrossScanMainActivity.class);
                        intent3.putExtra("flag","vehicle");
                        startActivity(intent3);
                        break;
                    case R.id.btn_self://私家車違規
                        Intent intent4 = new Intent(CommonformsActivity.this,CommonFormsPrivateCarActivity.class);
                        startActivity(intent4);
                        break;
                    case R.id.btn_title_left:
                        finish();
                        break;
                }}
    }



}
